<template>
    <div >
        <el-dialog v-model="updateDialogVisible"

         :destroy-on-close="true" :close-on-click-modal="false" title="修改图文内容" 
         @open="handleOpen"
         @close="handleClose">
            <el-form :model="content">
                <el-form-item label="标题" :label-width="formLabelWidth">
                    <el-input v-model="content.title" autocomplete="off" />
                </el-form-item>
                <el-form-item label="内容" :label-width="formLabelWidth">
                    <el-input v-model="content.text" :rows="4" type="textarea" autocomplete="off" />
                </el-form-item>
                <el-form-item label="选择类别" :label-width="formLabelWidth">
                    <el-select
                        v-model="content.type.id"
                        class="m-2"
                        placeholder="请选择类别"
                        size="large"
                    >
                        <el-option
                            v-for="item in types"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="上传" :label-width="formLabelWidth">
                    <Upload></Upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="updateDialogVisible = false"
                        >取消</el-button
                    >
                    <el-button
                        type="primary"
                        @click="handleUpdate"
                    >
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import { ElMessage  } from "element-plus";
import Upload from '@/components/Upload.vue';
import { defineProps,defineEmits,ref } from 'vue';
import { useContentStore } from "@/stores/content";
import { updateContent } from '@/services/content';
import { storeToRefs } from "pinia";
const contentStore = useContentStore();
const {fetchContents,fetchDeleteFiles} = contentStore;
const {content,contents,contentTypes,removeFileList,fileList,updateDialogVisible} = storeToRefs(contentStore);
// const emit = defineEmits(['show']);
const props = defineProps(['types']);
// const dialogFormVisible = ref(false);
const formLabelWidth = ref(100);
const isSubmited = ref(false);

const handleClose = () => {
    
    if(isSubmited.value){
        isSubmited.value = false;
    }else{
        removeFileList.value = [...removeFileList.value,...fileList.value.map(item => item.url)]
        
        let srcImages = content.value.images.map(item => item.path);
        
        removeFileList.value = removeFileList.value.filter(item => !srcImages.includes(item));
        
    }
    fileList.value = [];
    fetchDeleteFiles();
}
const handleUpdate = async () => {
    
    // if(fileList.value.length == 0){
    //     ElMessage.error('图片不能为空');
    //     return 
    // }
    // const images = fileList.value.map((item:any)=>({
    //     url:item.url
    // }));
    // const images = fileList.value.filter((item) => item.url).map((item) => ({
    //     url: item.url,
    // }));
    const images = fileList.value.filter(item => item.url).map(item => item.url);
    if(images.length == 0){
        ElMessage.error('图片不能为空');
        return 
    }
    
    // content.value.images = JSON.stringify(images);
    content.value.images = images;
    content.value.typeId = content.value.type.id;
    await updateContent(content.value);
    // emit('show',{current:contents.current});
    await fetchContents({current:contents.value.current});
    updateDialogVisible.value = false;
    isSubmited.value = true;
}
</script>
<style>
.add-btn{
    display: flex;
    justify-content: flex-end;
}
</style>